package cn.newhopedairy.delivery.applicationc.impl;

import cn.newhopedairy.common.wrapper.R;
import cn.newhopedairy.delivery.applicationc.DeliveryCmdService;
import cn.newhopedairy.delivery.applicationq.IDeliveryOrderService;
import cn.newhopedairy.delivery.infrastructure.db.po.DeliveryOrderPo;
import cn.newhopedairy.order.client.MainOrderClient;
import cn.newhopedairy.order.command.DeliveredCmd;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Neo.Li
 * @date 2020-10-0511:23
 */
@Slf4j
@Service
public class DeliveryCmdServiceImpl implements DeliveryCmdService {
    @Resource
    private MainOrderClient mainOrderClient;

    @Autowired
    private IDeliveryOrderService deliveryOrderService;

    @Override
    public boolean delivered(Long deliveryOrderId) {
        DeliveryOrderPo po = new DeliveryOrderPo();
        po.setId(deliveryOrderId);
        //设置送达状态
        po.setStatus(true);
        boolean result = deliveryOrderService.updateById(po);

        if(!result) {
            //TODO 更新剩余配送数量，如果是最后一次配送完成，更新订单的配送状态，这里需要保证最终一致性
            List<DeliveredCmd> deliveredCmds = new ArrayList<>();
            deliveredCmds.add(
                    DeliveredCmd.builder().goodsName(po.getGoodsName()).deliveredNum(po.getDeliveryCount()).build());
            R r = mainOrderClient.delivered(deliveredCmds, "123431");
            log.warn(r.getMsg());
        }
        return result;
    }

    @Override
    public boolean refuse(Long deliveryOrderId) {
        return true;
    }

    @Override
    public boolean lack(Long deliveryOrderId) {
        return true;
    }
}
